package com.sgb.contract.view.widget.pop;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.contract.R;
import com.sgb.contract.model.ContractKeyValueBean;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

/**
 * 选择pop弹窗
 * @author cyj
 */
public class SelectPop extends ContractPopWindow<ContractKeyValueBean> {

    public int currentPosition = 0;
    public boolean isClickItem;
    private SelectPopAdapter mAdapter;
    private AdapterOnItemClick<ContractKeyValueBean> listener;

    public SelectPop(Context context) {
        super(context, 0);
    }

    @Override
    protected int popLayout() {
        return R.layout.contract_pop_select_pop;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        RecyclerView recyclerView = holder.getPop().findViewById(R.id.goods_type_list);
        mAdapter = new SelectPopAdapter(mContext);
        mAdapter.setItemSelect(true);
        mAdapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(mAdapter);

        holder.getPop().findViewById(R.id.bg_pop_gray_layout).setOnClickListener(view -> dissMiss());
        mAdapter.setOnItemClick(new AdapterOnItemClick<ContractKeyValueBean>() {
            @Override
            public void onItemClick(ContractKeyValueBean contractKeyValueBean, int position) {
                isClickItem = true;
                currentPosition = position;
                if(null != listener) {
                    listener.onItemClick(contractKeyValueBean, position);
                }
            }
        });
    }

    @Override
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        super.setOnDismissListener(() -> {
            isClickItem = false;
            onDismissListener.onDismiss();
        });
    }

    public void setOnItemOnClickListener(AdapterOnItemClick<ContractKeyValueBean> listener) {
        this.listener = listener;
    }

    /**
     * 设置数据集
     * @param list
     */
    public void setData(List<ContractKeyValueBean> list) {
        list.add(0, getDefault());
        mAdapter.setDatas(list);
    }

    /**
     * 获取默认的bean
     * @return
     */
    private ContractKeyValueBean getDefault() {
        ContractKeyValueBean defaultBean = new ContractKeyValueBean();
        defaultBean.key = "-1";
        defaultBean.value = "全部";
        return defaultBean;
    }
}